import java.util.LinkedList;
import java.util.List;

// 1.
class MyThread extends Thread {
    @Override
    public void run() {
        System.out.println("aaa");
    }
}

class MyRunnable implements Runnable {

    @Override
    public void run() {
        System.out.println("bbb");
    }
}

// 懒汉模式
class SingletonLazy {
    private static SingletonLazy instance = new SingletonLazy();
    private SingletonLazy() {

    }
    public static SingletonLazy getInstance() {
        return instance;
    }
}

// 饿汉模式
class Singleton {
    // 保证内存可见性
    // 防止指令重排序
    private static volatile Singleton instance = null;
    private Singleton() {

    }

    public static Singleton getInstance() {
        // 这个 if 是要判断是否加锁, 因为加锁操作是要消耗大量的资源, 判断是否要加锁
        // 降低了锁的竞争的频率, 使得程序的执行效率更高
        if (instance == null) {
            synchronized (Singleton.class) {
                // 这个 if 是判断是否要实例化对象, 要是对象已经被实例化了, 就不用进去实例化对象了.
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}
public class Test {
    public static void main(String[] args) {
        MyThread t = new MyThread();
        t.start();
        Thread t1 = new Thread(new MyRunnable());
        t1.start();

        MyThread t3 = new MyThread() {
            @Override
            public void run() {
                System.out.println("aa");
            }
        };

        Thread t4 =  new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("jjhj");
            }
        });

        Thread t5 = new Thread(() -> {
            System.out.println("aaaa");
        });
    }


    public static void quickSort(int[] arr, int l, int r) {
        if (r - l < 1) {
            return;
        }
        int div = partition(arr, l, r);

        // 排序 div 左边的数
        quickSort(arr, l, div);
        // 排序 div 右边的数
        quickSort(arr, div+1, r);
    }

    private static int partition(int[] arr, int l, int r) {
        int i = l;
        int j = r;
        int pivot = arr[l];

        while (i < j) {
            while (i < j && arr[j] >= pivot) {
                j--;
            }
            arr[i] = arr[j];
            while (i < j && arr[i] >= pivot) {
                i++;
            }
            arr[j] = arr[i];
        }
        arr[i] = pivot;
        return i;
    }

}
